/**
 * Created by Administrator on 2016/12/15 0015.
 */
$(function(){
    /*..............................利用layui 创建分页和调用分页.............................*/
    layui.use('laypage', function() {
        var laypage = layui.laypage;
        var nums=15;
        //渲染当前页
        function render(curr,arrList){
            var currPageNums = curr*nums;//当前页的内容数量
            var userList = [];
            currPageNums = currPageNums >= arrList.length ? (arrList.length) :currPageNums;
            for(var i = (curr*nums - nums); i < currPageNums; i++){
                var createTime=new Date(arrList[i].createTime);
                arrList[i].createTime = formatDate(createTime);
                userList.push(arrList[i]);
            }
            return $("#userMemberTpl").render(userList);
        }
        //调用分页
        function getMemberPage(arrList){
            laypage({
                cont: 'pageDemo',
                pages: Math.ceil(arrList.length/nums),
                skin: '#5FB878' ,
                skip: true,
                jump: function(obj){
                    $("#memberList_content").html(render(obj.curr,arrList)) ;
                }
            })
        }
        //初始化页面
        function getInitPage (){
            EasyAjax.ajax_Get_Json({
                    url:"user/showUserList.json",
                    data:{number:"15"}
                },
                function(data) {
                    getMemberPage(data.data.userList);
                }
            );
        }
        getInitPage();
       /* 初始化等级模板*/
        EasyAjax.ajax_Get_Json({
            url:"user/getUseLevel.json"
        },function(data){
            //搜索等级模板
            var $searchWords = $("#searchTpl").render(data.data);
            //新增等级模板
            var $addLevelWords = $("#levelTpl").render(data.data);
            //编辑等级模板
            var $levelWords = $("#editor_levelTpl").render(data.data);
            $(".search_level").append($searchWords);
            $(".memberLever").append($addLevelWords);
            $(".editor_memberLever").append($levelWords);
            layui.use('form', function(){
                var form = layui.form();
            });
        });

        /*................................搜索会员.................................*/

        var $member_search=$(".member_search");
        var $search_userName=$(".search_userName");
        var $search_phone=$(".search_phone");
        var $search_startTime=$(".search_startTime");
        var $search_endTime=$(".search_endTime");
        var $search_qq=$(".search_qq");
        var $search_level=$(".search_level");
        $member_search.click(function(){
            var search_userNameValue=$search_userName.val();
            var search_phoneValue=$search_phone.val();
            var search_startTimeValue=$search_startTime.val();
            var search_endTimeValue=$search_endTime.val();
            var search_qqValue= $search_qq.val();
            var search_levelValue= $search_level.val();
            var searchValue={"name":search_userNameValue,"phone":search_phoneValue,"startTime":search_startTimeValue,
                "endTime":search_endTimeValue,"qq":search_qqValue,"level":search_levelValue};
            var searchValue_json=JSON.stringify(searchValue);
            EasyAjax.ajax_Post_Json({
                    url:"/user/getUseInfo.json",
                    data: {searchValue:searchValue_json}
                },
                function (data) {
                    var arrList=data.data;
                    $.each(arrList,function(index,time){
                        var createTime=new Date(time.createTime);
                        this.createTime=formatDate(createTime);
                    });
                    getMemberPage(arrList);
                });

        });

        /*...............................新增，编辑，删除会员功能.................................*/

        /*..........新增会员.................*/
            //获取新增会员的各类名
            var memberUserName=$(".memberUserName");
            var memberPhone=$(".memberPhone");
            var memberPassword=$(".memberPassword");
            var memberLevel=$(".memberLever");
            var memberDate=$(".memberDate");
            var memberQQ=$(".memberQQ");
            var $memberSave=$(".memberSave");
            var $memberReset=$(".memberReset");



            //用户名长度为2-12位字符
            memberUserName.on("blur",function(){
                var $this=$(this),
                    $length=$.trim($this.val()).length;
                if ($length<2||$length>12) {
                    $this.addClass("error");
                    layer.msg("名字格式不正确");
                    $(".use_tips").addClass("active");

                }else{
                    $this.removeClass("error");
                    $(".use_tips").removeClass("active");
                }
            });
            //密码长度为6-16位字符
            memberPassword.on("blur",function(){
                var $this=$(this),
                    $length=$this.val().length;
                if ($length<6||$length>16) {
                    $this.addClass("error");
                    layer.msg("密码长度不符合要求");
                    $(".pwd_tips").addClass("active");
                }else{
                    $this.removeClass("error");
                    $(".pwd_tips").removeClass("active");
                }
            });
            //手机号码格式验证
            memberPhone.on("blur",function(){
                var $this=$(this),
                    $phoneNumVal=$this.val();
                if(!(/^1[345678]\d{9}$/.test($phoneNumVal))){
                    $this.addClass("error");
                    layer.msg("手机格式不正确");
                    $(".ph_tips").addClass("active");
                }else{
                    $this.removeClass("error");
                    $(".ph_tips").removeClass("active");
                }
        });

            //新增会员方法
            function addMember(){
                var userName= memberUserName.val();
                var phoneNum=memberPhone.val();
                var password=memberPassword.val();
                var qq=memberQQ.val();
                var level=memberLevel.val();
                //提交校验规则
                if ($.trim(userName).length<2||$.trim(userName).length>12) {
                    memberUserName.addClass("error");
                    return false;
                }else if ($.trim(password).length<6||$.trim(password).length>16){
                    memberPassword.addClass("error");
                    return false;
                }else if (!(/^1[345678]\d{9}$/.test(phoneNum))){
                    memberPhone.addClass("error");
                    return false;
                }else{
                    EasyAjax.ajax_Post_Json({
                        url:"user/registerByAdmin.json",
                        data:{userName:userName,phoneNum:phoneNum,password:password,level:level,QQ:qq}
                    },function(data){
                        clearForm();
                        layer.msg("增加成功！");
                       getInitPage();
                    })
                }
            }
            //保存新增内容
            $memberSave.on("click",function(){
                if(memberUserName.val()==""||memberPhone.val()==""||memberPassword.val()==""){
                    memberUserName.addClass("error");
                    memberPassword.addClass("error");
                    memberPhone.addClass("error");
                    layer.msg("请把必填内容填写完整");
                }else{
                    addMember();
                }

            });
            //清除内容
            function clearForm(){
                $(".memberUserName").val("");
                $(".memberPhone").val("");
                $(".memberPassword").val("");
                $(".memberDate").val("");
                $(".memberQQ").val("")
            }
            //重置内容
            $memberReset.on("click",function(){
                clearForm()
            });

            //点击增加按钮弹出新增页面
            var $member_increase=$(".member_increase");
            $member_increase.click(function(){
                layer.open({
                    type: 1,
                    fixed: false,
                    area: ['800px', '520px'],
                    content:$(".increase_memberlist")
                });
            });

            /*..............编辑会员.......................*/

            var $editor_memberUserName=$(".editor_memberUserName");
            var $editor_memberPhone=$(".editor_memberPhone");
            var $editor_memberQQ=$(".editor_memberQQ");
            var $chose_level=$(".chose_level");
            var $editor_id="";

            //点击编辑按钮弹出编辑框
            $(document).on("click",".member_editor",function(){
                layer.open({
                    type: 1,
                    fixed: false,
                    area: ['800px', '460px'],
                    content: $('.editor_memberlist')
                });
                var editor_userName=$(this).parents("tr:first").find(".name").html();
                var editor_phone=$(this).parents("tr:first").find(".phone").html();
                var editor_qq=$(this).parents("tr:first").find(".qq").html();
                var editorLevel=$(this).parents("tr:first").find(".editorLevel").html();
                var editor_id=$(this).parents("tr:first").attr("id");

                $editor_memberUserName.val(editor_userName);
                $editor_memberPhone.val(editor_phone);
                $editor_memberQQ.val(editor_qq);
                $chose_level.find("dd[lay-value="+editorLevel+"]").click();
                $editor_id=editor_id;

        });

        //点击保存
        $(".editor_memberSave").on("click",function(){
            var editor_level=$chose_level.find(".layui-this").attr("lay-value");
            EasyAjax.ajax_Post_Json({
                    url:"user/updateUserLevel.json",
                    data:{id:$editor_id,level:editor_level}
                },
                function(data) {
                    console.log(data)
                    layer.msg("修改成功！");
                    $(".layui-layer-close").click();
                    getInitPage();
                }
            );
        });


            /*.................删除会员...............*/

            $(document).on("click","#memberList_content .member_delet",function(){
                var del_member =$(this).parents("tr:first").attr("id");
                layer.confirm('确认删除', {
                    btn: ['确认','取消'],
                    anim: 6
                }, function(){
                    EasyAjax.ajax_Post_Json({
                            url:"user/delUser.json",
                            data: {id:del_member}
                        },
                        function (data) {
                            layer.msg("删除成功！");
                            getInitPage();
                        });
                },function(){
                });
            });

        });


            //格式化时间
            function formatDate(date){
                var year=date.getFullYear();
                var month=date.getMonth()+1;
                var day=date.getDate();
                var hour=date.getHours();
                var minute=date.getMinutes();
                if(minute<10){
                    minute="0"+minute;
                }
                var second=date.getSeconds();
                if(second<10){
                    second="0"+second;
                }
                return year+"/"+month+"/"+day+" "+hour+":"+minute+":"+second;
            }

})